package com.ukayunnuo.controller;

import com.ukayunnuo.core.Result;
import com.ukayunnuo.domain.entity.UserInfo;
import com.ukayunnuo.service.UserInfoService;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author yunnuo
 * @since 1.0.0
 */
@RequestMapping("/tdengine/test/userinfo")
@RestController
public class UserInfoController {

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private JdbcTemplate tdengineJdbcTemplate;

    @GetMapping("/selectByTs")
    public Result<List<UserInfo>> selectByTs(Integer days) {
        // 使用mybatis进行查询
        return Result.success(userInfoService.selectByTs(days));
    }

    @GetMapping("/getByUserId/{id}")
    public Result<UserInfo> getByUserId(@PathVariable Long id) {
        // 使用mybatis进行查询
        return Result.success(userInfoService.getByUserId(id));
    }

    @GetMapping("/getByUserId2/{id}")
    public Result<List<UserInfo>> getByUserId2(@PathVariable String id) {
        // 使用tdengineJdbcTemplate进行id查询
        return Result.success(userInfoService.getByUserIdForTemplate(id));
    }

    @PostMapping("/save")
    public Result<Boolean> save(@RequestBody UserInfo userInfo) {
        userInfo.setTs(Timestamp.valueOf(LocalDateTime.now()));
        userInfo.setCreateTime(Timestamp.valueOf(LocalDateTime.now()));
        return Result.success(userInfoService.save(userInfo));
    }

    @PostMapping("/save2")
    public Result<Boolean> save2(@RequestBody UserInfo userInfo) {
        return Result.success(userInfoService.saveForTemplate(userInfo));
    }

}
